'use client'; import { yupResolver } from '@hookform/resolvers/yup'; import { SubmitHandler, useForm } from 'react-hook-form'; import { AccountProfileFormType } from '@akinon/next/types'; import * as yup from 'yup'; import { Button, Checkbox, Input, Modal, Radio, Select, Link } from '@theme/components'; import { useAppSelector } from '@akinon/next/redux/hooks'; import { useEffect, useState } from 'react'; import { useUpdateProfileMutation, useGetProfileInfoQuery } from '@akinon/next/data/client/account'; import { ROUTES } from '@theme/routes'; import { useLocalization } from '@akinon/next/hooks'; import { Trans } from '@akinon/next/components/trans'; const accountProfileFormSchema = (t) => yup.object().shape({ first_name: yup .string() .required(t('account.my_profile.form.required')) .matches( /^[a-zA-ZğüşöçıİĞÜŞÖÇ\s]+$/, t('account.my_profile.form.error.name_match') ), last_name: yup .string() .required(t('account.my_profile.form.required')) .matches( /^[a-zA-ZğüşöçıİĞÜŞÖÇ\s]+$/, t('account.my_profile.form.error.surname_match') ), birthdate: yup .object() .shape({ day: yup.number().required(t('account.my_profile.form.required')), month: yup.number().required(t('account.my_profile.form.required')), year: yup.number().required(t('account.my_profile.form.required')) }) .test( 'isValidBirthdate', t('account.my_profile.form.birth_date.error.not_valid'), (value) => { const { day, month, year } = value; const date = new Date(`${year}-${month}-${day}`); return date && date <= new Date(); } ), phone: yup .string() .transform((value: string) => value?.replace(/_/g, '').replace(/ /g, '')) .length(11, t('account.my_profile.form.phone.error.not_valid')) .required(t('account.my_profile.form.required')), gender: yup.string().required(t('account.my_profile.form.required')) }); export default function Page() { const { t } = useLocalization(); const { user_phone_format } = useAppSelector((state) => state.config); const { data: profileInfo } = useGetProfileInfoQuery(); const [updateProfile] = useUpdateProfileMutation(); const [responseMessage, setResponseMessage] = useState({ title: '', content: '' }); const [isModalOpen, setIsModalOpen] = useState(false); const { register, handleSubmit, control, reset, formState: { errors } } = useForm({ resolver: yupResolver(accountProfileFormSchema(t)) }); const handleSuccess = () => { setResponseMessage({ title: t('account.my_profile.form.success.title').toString(), content: t('account.my_profile.form.success.description').toString() }); setIsModalOpen(true); }; const handleError = () => { setResponseMessage({ title: t('account.my_profile.form.error.title').toString(), content: t('account.my_profile.form.error.description').toString() }); setIsModalOpen(true); }; const handleModalClick = () => { setIsModalOpen(false); }; const onSubmit: SubmitHandler = async (data) => { const dateOfBirth = `${data.birthdate.year}-${data.birthdate.month}-${data.birthdate.day}`; if ('birthdate' in data) { delete data.birthdate; } updateProfile({ date_of_birth: dateOfBirth, ...data }) .unwrap() .then(handleSuccess) .catch(handleError); }; const buildOptionDays = () => { const days = Array.from({ length: 31 }, (_, index) => { const day = (index + 1).toString().padStart(2, '0'); return { label: day, value: day }; }); return days; }; const buildOptionMonths = () => { const months = [ 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december' ]; const options = months.map((month, index) => { const monthValue = (index + 1).toString().padStart(2, '0'); return { label: t(`account.my_profile.form.months.${month}`), value: monthValue }; }); return options; }; const buildOptionYears = () => { const currentYear = new Date().getFullYear(); const minOffset = 0; const maxOffset = 80; const years = Array.from( { length: maxOffset - minOffset + 1 }, (_, index) => { const year = currentYear - index; return { label: year, value: year }; } ); return years; }; useEffect(() => { const todayDate = new Date().toISOString().slice(0, 10); const { first_name = '', last_name = '', phone = '', gender = '', email_allowed = false, sms_allowed = false, date_of_birth = todayDate } = profileInfo || {}; const [year, month, day] = date_of_birth?.split('-') ?? todayDate.split('-'); reset({ first_name, last_name, phone, gender, email_allowed, sms_allowed, birthdate: { day, month, year } }); }, [profileInfo, reset]); return (

{t('account.my_profile.header.title')}

{t('account.my_profile.header.subtitle')}